/*
 * Copyright 2013 Glassy Cinema.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package hu.glassy.web.panel.header;

import hu.glassy.core.model.Account;
import hu.glassy.web.auth.ApplicationSession;
import hu.glassy.web.page.home.PublicHomePage;
import hu.glassy.web.page.movie.edit.MovieAddPage;
import hu.glassy.web.page.movie.list.MovieListPage;
import hu.glassy.web.page.person.edit.PersonAddPage;
import hu.glassy.web.panel.account.header.UserDetailsPanel;
import hu.glassy.web.panel.common.CustomPanel;
import hu.glassy.web.panel.header.login.LoginPanel;
import org.apache.wicket.authroles.authentication.AuthenticatedWebSession;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.EmptyPanel;
import org.apache.wicket.model.IModel;

/**
 * Fejléc panel. Ha nincs bejelentkezett felnasználó, akkor a
 * {@link LoginPanel}-t jeleníti meg, ha van bejelentkezett felhasználó, akkor a
 * {@link UserDetailsPanel}-t jeleníti meg.
 *
 * @author molnar.alexander@sonrisa.hu
 */
public class HeaderPanel extends CustomPanel<Void>
{

  private static final long serialVersionUID = 9035371498873723729L;

  public HeaderPanel(final String id)
  {
    super(id);

    add(new BookmarkablePageLink<String>("homeLink", PublicHomePage.class));
    add(new EmptyPanel("searchBox"));

    if (AuthenticatedWebSession.get().isSignedIn())
    {
      add(new UserDetailsPanel("accountBox"));
    }
    else
    {
      add(new LoginPanel("accountBox"));
    }

    boolean isAdmin = false;
    IModel<Account> loggedInUser = ApplicationSession.get().getAccount();
    if (loggedInUser != null)
    {
      isAdmin = loggedInUser.getObject().isActive() && loggedInUser.getObject().isAdmin();
    }

    Link movieListLink = new BookmarkablePageLink("movieListLink", MovieListPage.class);
    movieListLink.setVisible(isAdmin);
    add(movieListLink);

    Link movieAddLink = new BookmarkablePageLink("movieAddLink", MovieAddPage.class);
    movieAddLink.setVisible(isAdmin);
    add(movieAddLink);

    Link personAddLink = new BookmarkablePageLink("personAddLink", PersonAddPage.class);
    personAddLink.setVisible(isAdmin);
    add(personAddLink);
  }
}
